package top.mschen.blog.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Component
public class PropertiesUtils {

    private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtils.class);

    private static Map<String, Object> map;

    static{
        map= new HashMap<String, Object>();
        Resource resource = new ClassPathResource("conf.properties");
        try {
            LOG.info("   获取配置文件的路径: {}",resource.getURL());
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            Enumeration<String> enus = (Enumeration<String>) properties.propertyNames();
            while (enus.hasMoreElements()){
                String key = (String) enus.nextElement();
                String value = properties.getProperty(key);
                map.put(key, value);
            }
            LOG.info("   加载的配置{}",map);
        } catch (IOException e) {
            LOG.error("  获取配置conf.properties 的配置文件异常!",e);
        }
    }

    public Object getValue(String name){
        return map.get(name);
    }
}
